﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminTitles
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminTitles_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        Me.txtTitle.IsEnabled = False
        Me.drpGemder.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub
    Private Sub refreshList()
        Me.grdTitles.ItemsSource = Admin.TitleList
        Me.grdTitles.Columns(0).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        Me.txtTitle.IsEnabled = True
        Me.drpGemder.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtTitle.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtTitle.Text = String.Empty
        Me.drpGemder.SelectedIndex = -1
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        Me.txtTitle.IsEnabled = False
        Me.drpGemder.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        grdTitles.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        ' ClearControls()
        Me.txtTitle.IsEnabled = True
        Me.drpGemder.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub grdTitles_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdTitles.SelectionChanged
       
        If Me.grdTitles.SelectedItems.Count = 0 Then
            Me.grdTitles.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdTitles.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Dim selectedRow As DataRow = grdTitles.SelectedItem
            If Not IsNothing(selectedRow) Then
                txtTitle.Text = selectedRow(1).ToString()
                If selectedRow(2).ToString().ToUpper = "MALE" Then
                    drpGemder.SelectedIndex = 1
                ElseIf selectedRow(2).ToString().ToUpper = "FEMALE" Then
                    drpGemder.SelectedIndex = 2
                ElseIf selectedRow(2).ToString().ToUpper = "EITHER" Then
                    drpGemder.SelectedIndex = 0
                End If
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtTitle.Text.Trim = String.Empty Then
            MsgBox("Please enter Title !")
            txtTitle.Focus()
            Exit Sub
        ElseIf drpGemder.SelectedIndex = -1 Then
            MsgBox("Please select department !")
            Exit Sub
        Else
            btnSave.IsEnabled = False

            'grdConsult.Enabled = True
            Dim sql As StringBuilder
            Dim intGender As Integer

            If Me.drpGemder.SelectedValue.ToString.ToUpper = "MALE" Then
                intGender = 1
            ElseIf Me.drpGemder.SelectedValue.ToString.ToUpper = "FEMALE" Then
                intGender = 2
            ElseIf Me.drpGemder.SelectedValue.ToString.ToUpper = "EITHER" Then
                intGender = 0
            End If
            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder
                    With sql
                        .Append("")
                        .Append("insert into l_titles ")
                        .Append("(title_text,title_gender)")
                        .Append(" Values(")
                        .Append(" '" & Me.txtTitle.Text & "',")
                        .Append(intGender & ")")
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The new Title has been added successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error adding the Title to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If
                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update l_titles set ")
                        .Append(" title_text = '" & Me.txtTitle.Text & "',")
                        .Append(" title_gender = " & intGender)
                        Dim selectedRow As DataRow = grdTitles.SelectedItem
                        .Append(" where Title_ID = " & selectedRow(0))
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The Title's details have been updated successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error in editing the Title's details. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If

            End Select
            ClearControls()
            txtTitle.IsEnabled = False
            drpGemder.IsEnabled = False
            grdTitles.IsEnabled = True
            btnCancel.IsEnabled = False
            btnAddNew.IsEnabled = True
            btnEdit.IsEnabled = False
            btnCancel.IsEnabled = False
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete qualification ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRow = grdTitles.SelectedItem
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete  from l_titles where Title_ID=" & selectedRow(0).ToString())
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdTitles.IsEnabled = True
                ClearControls()
                refreshList()
                MsgBox("The Title Details has been deleted successfully.", MsgBoxStyle.Information, "Success")
            Else
                MsgBox("There is an error while deleting the Title to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If

    End Sub
End Class
